﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Messaging;
using System.Windows;
using ModelLayer;
using Toolbox;

namespace FremtidensPlejehjem_Infoskærm.Messaging
{
    public class GeneralMessaging
    {
        MessageQueue _sendQueue;
        MessageQueue _receiveQueue;

        public GeneralMessaging()
        {
            _sendQueue = CreateQueue("IS#-Send");
            _receiveQueue = CreateQueue("IS#-Reply"); //Replace "#" with the instance number / unique identifier for the specific instance
        }

        private MessageQueue CreateQueue(string queueName)
        {
            // Create queue
            string path = @".\Private$\" + queueName;
            MessageQueue mq = new MessageQueue();

            if (MessageQueue.Exists(path))
                mq = new MessageQueue(path);
            else
                mq = MessageQueue.Create(path);

            return mq;
        }

        public List<Activity> Get5NextActivities()
        {
            List<Activity> result = null;

            Producer producer = new Producer(_sendQueue);
            CommandMessage command = new CommandMessage("Get5NextActivities", null, "Activity");

            producer.SendMessage(command);

            Consumer consumer = new Consumer(_receiveQueue);
            Message reply = (Message)consumer.ReceiveMessage();

            result = GeneralTools.DeserializeMessage<List<Activity>>(reply);

            return result;
        }


    }
}